package com.huarui.purchaseOrder.action;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.ewaf.framework.action.BaseAction;

import com.huarui.purchaseOrder.model.PurOrSaleOrder;
import com.huarui.purchaseOrder.model.PurOrSaleOrderDetail;
import com.huarui.purchaseOrder.service.PurchaseOrderService;
import com.huarui.saleOrder.service.SaleOrderService;

public class PurchaseTakeOverAction extends BaseAction{

	public static final String MESS = "mess";
	
	private static final long serialVersionUID = 4189851057188050361L;

	private Logger logger = Logger.getLogger(PurchaseTakeOverAction.class);
	
	private PurchaseOrderService purchaseOrderService;
	
	public void setPurchaseOrderService(PurchaseOrderService purchaseOrderService) {
		this.purchaseOrderService = purchaseOrderService;
	}

	private Integer ordersId;
	
	private String pur;
	private String all;
	private String update;
	private String add;
	
	private Map<String, Object> mesMap;
	
	public Map<String, Object> getMesMap() {
		return mesMap;
	}

	public void setMesMap(Map<String, Object> mesMap) {
		this.mesMap = mesMap;
	}

	public Integer getOrdersId() {
		return ordersId;
	}

	public void setOrdersId(Integer ordersId) {
		this.ordersId = ordersId;
	}

	public String getPur() {
		return pur;
	}

	public void setPur(String pur) {
		this.pur = pur;
	}

	public String getAll() {
		return all;
	}

	public void setAll(String all) {
		this.all = all;
	}

	public String getUpdate() {
		return update;
	}

	public void setUpdate(String update) {
		this.update = update;
	}

	public String getAdd() {
		return add;
	}

	public void setAdd(String add) {
		this.add = add;
	}

	public String takeOverOrders() {
		mesMap = purchaseOrderService.takeOverSaleOrder(ordersId);
		return MESS;
	}
	
	@SuppressWarnings("unchecked")
	public String takeOverUpdateOrSubmit() {
		JSONArray ja_all = JSONArray.fromObject(all);
		JSONArray ja_update = JSONArray.fromObject(update);
		JSONArray ja_add = JSONArray.fromObject(add);
		JSONObject jo_pur = JSONObject.fromObject(pur);
		
		PurOrSaleOrder obj_pur = (PurOrSaleOrder) JSONObject.toBean(jo_pur, PurOrSaleOrder.class);
		List<Integer> list_all = (List<Integer>) JSONArray.toCollection(ja_all);
		List<PurOrSaleOrderDetail> list_add = (List<PurOrSaleOrderDetail>) JSONArray.toCollection(ja_add);
		List<PurOrSaleOrderDetail> list_update = (List<PurOrSaleOrderDetail>) JSONArray.toCollection(ja_update);
		
		mesMap = purchaseOrderService.takeOverUpdateOrSubmit(obj_pur, list_update, list_add, list_all);
		return MESS;
	}
}
